package com.jinke.api.common.util;

import com.jinke.api.modules.app.model.OrderParcel;
import com.jinke.api.modules.app.response.UserOrderSummaryVO;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.List;

public class FileConverter {

    public static byte[] listToExcel(List<UserOrderSummaryVO> summaryVOS) {
        try (Workbook workbook = new XSSFWorkbook()) {
            Sheet sheet = workbook.createSheet("Summary");

            int rowNum = 0;
            for (UserOrderSummaryVO summaryVO : summaryVOS) {
                String orderDate = summaryVO.getOrderDate();
                List<OrderParcel> parcelByOrderIdList = summaryVO.getParcelByOrderIdList();

                sheet.createRow(rowNum++).createCell(0).setCellValue("日期：" + orderDate);
                for (OrderParcel parcel : parcelByOrderIdList) {
                    if (parcel!=null){

                        String trackingNumber = parcel.getTrackingNumber();
                        if (trackingNumber!=null){

                            sheet.createRow(rowNum++).createCell(0).setCellValue(trackingNumber);
                        }else {
                            sheet.createRow(rowNum++).createCell(0).setCellValue("");

                        }
                    }else {
                        System.out.println("22222222222222");
                    }
                }
            }

            try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
                workbook.write(baos);
                return baos.toByteArray();
            }
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }

}
